package logic

import (
	"context"
	"errors"
	"product/model/mysql"
	"time"

	"product/productrpc/internal/svc"
	"product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopDetailLogic {
	return &ShopDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopDetailLogic) ShopDetail(in *productrpc.ShopDetailRequest) (*productrpc.ShopDetailResponse, error) {
	// todo: add your logic here and delete this line

	var shop mysql.Shop
	mysql.DB.Debug().Where("id = ?", in.ShopId).Find(&shop)
	if shop.ID == 0 {
		return nil, errors.New("商品不存在，不允许查看")
	}

	return &productrpc.ShopDetailResponse{
		ShopId:    int64(shop.ID),
		ShopName:  shop.ShopName,
		ShopPrice: float32(shop.ShopPrice),
		CreatedAt: shop.CreatedAt.Format(time.DateTime),
	}, nil
}
